<?php
if( !defined( 'APP_ROOT' ) ) require_once '../private/path.conf';
USE_LIB('page');
USE_PAGE('index');

class PageRecherche extends Page
	{
	private $m_f_gskwds;
	private $m_modules;
	
	protected function build()
		{
		$this->m_f_gskwds = trim(GETVAL( 'f_gskwds', $_POST['f_gskwds'] ));
		$this->m_modules = array( 
			'products' => array('build'=>'new Products()','url'=>'default'),
			'bills' => array('build'=>'new Bills()','url'=>'default'),
			'newsletter_inscrits' => array('build'=>'new Newsletter_inscrits()','url'=>'default'),
			);
		}
		
		
	public function parseTemplate( &$tpl )
		{
		$moduleplus = array(
/*
			'pages' => array('url'=>'preferences/pages_edit,'),
			'blockhtml' => array('url'=>'preferences/blockhtml_edit,'),
			'blocktext' => array('url'=>'preferences/blocktext_edit,'),
*/
			);
		$this->m_modules = array_merge( $this->m_modules, $moduleplus );
		$tpl->setVarText( 'GENERAL_SEARCH_KEYWORDS', $this->m_f_gskwds );
		$total = 0;
		if( !empty($this->m_f_gskwds) && $tpl->loopExist('EACH_MODULES'))
			{
			$tpl->startLoop('EACH_MODULES');
			foreach( $this->m_modules as $name => $params )
				{
				$url = $params['url'];
				USE_APP($name);
				$extras = ARRAYVAL($params,'build',null);
				if( isset( $extras ) )
					eval("\$obj = new $name( 'VAR', $extras );" );
				else
					$obj = new $name( 'VAR' );
				$module = $obj->getSearchParams();
				$module['url'] = $url;
				$tpl->newLoop( 'EACH_MODULES' );
				$tpl->setVar( 'MODULE_TITLE', ARRAYVAL($module,'title') );
				if( ARRAYVAL($module,'url','default') == 'default' )
					$tpl->setVar( 'MODULE_URL', $name . '/' . $name . '_edit,' );
				else
					$tpl->setVar( 'MODULE_URL', ARRAYVAL($module,'url') );
				$obj->perPage(10,$this);
				$pro =& $obj->getLocalSearchProcessor();
				$pro->makeSearch($this->m_f_gskwds);
				$tpl->setVar( 'VAR_F_HEAD', ARRAYVAL($module,'head') );
				$tpl->setVarText( 'VAR_F_BODY', ARRAYVAL($module,'body') );
				$card = $obj->getNumElements();
				$tpl->setVarNumber( 'MODULE_SEARCH_CARD', $card );
				$total += $card;
				$obj->parseTemplate( $tpl );
				}
			$tpl->endLoop('EACH_MODULES');
			$tpl->setVarNumber( 'GENERAL_SEARCH_RESULT', $total );
			}
		$tpl->setVarNumber( 'GENERAL_SEARCH_RESULT', 0 );
		//$tpl->formatWords( $pro->getKeywords($this->m_f_gskwds),'<strong class="b-res">','</strong>' );
		parent::parseTemplate( $tpl, $formname, $args );
		}

	protected function getPageUrl() { return '/recherche,'.urlencode($this->m_f_gskwds); }
	protected function getBreadcrumbTitle() { return 'recherche de "'.$this->m_f_gskwds.'"'; }
	protected function getPageParent() { return 'PageIndex'; }
	}
?>